package mediator;

import guiWindows.GUISelectActivity;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JTextField;

import objectsDiagram.Fluent;
import objectsDiagram.Task;

public class SelectActivityMediator implements ActionListener {
	private GUISelectActivity guiSelectActivity;
	private Vector<Task> vectorTask;
	private JTextField parameterField;
	private TemplateMediator templateMediator;
	private LinkedList<Fluent> listFluents;
	
	public SelectActivityMediator(JTextField parameterField, Vector<Task> vectorTask, TemplateMediator newTemplateMediator){
		init(parameterField,newTemplateMediator);
		this.vectorTask = vectorTask;
		this.loadActivities();
	}
	
	public SelectActivityMediator(JTextField taskField,LinkedList<Fluent> listFluents, TemplateMediator newTemplateMediator) {
		init(taskField,newTemplateMediator);
		this.listFluents = listFluents;
		this.loadFluents();
	}
	
	private void init(JTextField taskField,TemplateMediator newTemplateMediator){
		this.parameterField = taskField;
		this.templateMediator = newTemplateMediator;
		this.guiSelectActivity = new GUISelectActivity();
		this.guiSelectActivity.setListenerButtons(this);
	}

	public void show(){
		this.guiSelectActivity.setVisible(true);
	}
	
	public void loadActivities(){
		Task task = null;
		for(int i=0;i<vectorTask.size();i++){
			task = vectorTask.get(i);
			this.guiSelectActivity.getListActivities().addItem(task.getId()+".start");
			this.guiSelectActivity.getListActivities().addItem(task.getId()+".end");
		}
	}
	
	public void loadFluents(){
		Fluent fluent = null;
		for(int i=0;i<listFluents.size();i++){
			fluent = listFluents.get(i);
			this.guiSelectActivity.getListActivities().addItem(fluent.getId());
		}
	}
	
	public void actionPerformed(ActionEvent e) {
		if(this.guiSelectActivity.getOkButton() == e.getSource()){
			parameterField.setText((String)this.guiSelectActivity.getListActivities().getSelectedItem());
			this.guiSelectActivity.setVisible(false);
			this.guiSelectActivity.dispose();
			this.templateMediator.setEnablerButtonInt(this.templateMediator.getEnablerButtonInt()-1);
			if(this.templateMediator.getEnablerButtonInt()==0)
				this.templateMediator.getGuiTemplatesSelector().getOkButton().setEnabled(true);
		}
		if(this.guiSelectActivity.getCancelButton() == e.getSource()){
			this.guiSelectActivity.setVisible(false);
			this.guiSelectActivity.dispose();
		}
	}

}
